package org.openkanban.client.ui.adminpanel;

import org.openkanban.client.ui.board.OpenKanbanButton;

import com.smartgwt.client.types.Overflow;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.layout.SectionStack;
import com.smartgwt.client.widgets.layout.SectionStackSection;

/**
 * GUI Element for the UserAccountWindow
 */
public class UserAccountWindow extends Window {

    private static final int FORM_WIDTH = 450;

    public UserAccountWindow() {
	SectionStack sectionStack = new SectionStack();

	SectionStackSection userAccountSection = new SectionStackSection("account");
	SectionStackSection userPasswordSection = new SectionStackSection("password");
	SectionStackSection userAvatarSection = new SectionStackSection("avatar");

	// user account section
	UserAccountForm userAccountForm = new UserAccountForm(FORM_WIDTH);
	IButton submitAccountButton = new OpenKanbanButton("update account");
	submitAccountButton.addClickHandler(new SubmitFormClickHandler(userAccountForm));
	userAccountSection.setItems(userAccountForm, submitAccountButton);

	// user password section
	UserAccountPasswordForm userPasswordForm = new UserAccountPasswordForm(FORM_WIDTH);
	IButton submitPasswordButton = new OpenKanbanButton("update password");
	submitPasswordButton.addClickHandler(new SubmitFormClickHandler(userPasswordForm));
	userPasswordSection.setItems(userPasswordForm, submitPasswordButton);

	// user avatar section
	UserAccountAvatarForm userAvatarForm = new UserAccountAvatarForm(FORM_WIDTH);
	IButton submitAvatarButton = new OpenKanbanButton("update avatar");
	submitAvatarButton.addClickHandler(new SubmitFormClickHandler(userAvatarForm));
	userAvatarSection.setItems(userAvatarForm, submitAvatarButton);

	sectionStack.setAnimateSections(true);
	sectionStack.setOverflow(Overflow.AUTO);
	sectionStack.setHeight100();
	sectionStack.setWidth(FORM_WIDTH);

	sectionStack.setSections(userAccountSection, userPasswordSection, userAvatarSection);

	setTitle("User Account");
	setWidth(FORM_WIDTH);
	setHeight(295);
	setAutoCenter(true);
	setShowMinimizeButton(false);
	setShowMaximizeButton(false);
	setIsModal(true);
	setShowModalMask(true);

	addItem(sectionStack);
    }

    private class SubmitFormClickHandler implements ClickHandler {

	private DynamicForm form;

	public SubmitFormClickHandler(DynamicForm form) {
	    super();
	    this.form = form;
	}

	@Override
	public void onClick(ClickEvent event) {
	    form.submit();
	}

    }

}
